// 文件路径: src/main/java/com/ruoyi/chat/mapper/ChatMessageMapper.java

package com.reliable.service.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reliable.common.annotation.DataSource;
import com.reliable.common.enums.DataSourceType;
import com.reliable.service.domain.ChatMessage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 聊天记录 Mapper
 */
@Mapper
@DataSource(value = DataSourceType.PG)
public interface ChatMessageMapper extends BaseMapper<ChatMessage> {

    // 1. 按 session_id 分页查询
    @DataSource(value = DataSourceType.PG)
    List<ChatMessage> selectBySessionId(@Param("sessionId") String sessionId,
                                        @Param("pageSize") int pageSize,
                                        @Param("offset") int offset);

    // 2. 游标分页
    @DataSource(value = DataSourceType.PG)
    List<ChatMessage> selectBySessionIdWithCursor(@Param("sessionId") String sessionId,
                                                  @Param("lastCreatedAt") LocalDateTime lastCreatedAt,
                                                  @Param("limit") int limit);

    // 3. 按 task_id 查询
    @DataSource(value = DataSourceType.PG)
    List<ChatMessage> selectByTaskId(@Param("taskId") Long taskId);

    // 4. 统计消息数
    @DataSource(value = DataSourceType.PG)
    Long countBySessionId(@Param("sessionId") String sessionId);

    // 5. 批量插入
    @DataSource(value = DataSourceType.PG)
    void insertBatch(@Param("list") List<ChatMessage> messages);

    // 6. 按时间范围查询
    @DataSource(value = DataSourceType.PG)
    List<ChatMessage> selectByTimeRange(@Param("startTime") LocalDateTime startTime,
                                        @Param("endTime") LocalDateTime endTime,
                                        @Param("limit") int limit);
                                        
    // 7. 查询所有不同的session_id
    @DataSource(value = DataSourceType.PG)
    List<String> selectDistinctSessionIds();
    
    // 8. 根据任务ID统计相关信息
    @DataSource(value = DataSourceType.PG)
    List<Map<String, Object>> getTaskStatisticsByTaskId(@Param("taskId") Long taskId);
}